package org.lanqiao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.lanqiao.dao.WxuserGiftDao;
import org.lanqiao.entity.Gift;
import org.lanqiao.entity.WxuserGift;
import org.lanqiao.mapper.GiftMapper;
import org.lanqiao.service.GiftService;
import org.lanqiao.util.Result;
import org.lanqiao.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author andenj
 * @describe
 * @data
 */
@Service
public class GiftServiceImpl implements GiftService {
    @Autowired
    GiftMapper giftMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    WxuserGiftDao wxuserGiftDao;

    @Override
    public List getFGift(String wxId) {
        Object o = redisTemplate.opsForValue().get(StringUtil.all_gift.concat(wxId));
        if(o!=null){
            return JSONObject.parseArray(o.toString(),String.class);
        }else{
            List list = giftMapper.getFGift(wxId);
            //发送到消息队列
            jmsTemplate.convertAndSend(StringUtil.gift_query_queue, JSON.toJSONString(new  HashMap(){{
                this.put("data",list);
                this.put("wxId",wxId);
            }}));
            return list;
        }
    }

    @Override
    public List getGiftByAll(String wxId ,String activityId){
        Object o = redisTemplate.opsForValue().get(StringUtil.gift_user.concat(wxId).concat("#").concat(activityId));
        if(o!=null){
            return JSONObject.parseArray(o.toString(),String.class);
        }else{
            List list = giftMapper.getGiftByAll(wxId,activityId);
            //发送到消息队列
            jmsTemplate.convertAndSend(StringUtil.gift_query_queue_user, JSON.toJSONString(new HashMap(){{
                this.put("data",list);
                this.put("wxId",wxId);
                this.put("activityId",activityId);
            }}));
            return list;
        }
    }

    @Override
    public List getRecords(String wxId) {
        Object o = redisTemplate.opsForValue().get(StringUtil.records.concat(wxId));
        if (o != null) {
            return JSONObject.parseArray(o.toString(), String.class);
        } else {
            List list = giftMapper.getRecords(wxId);
            //发送到消息队列
            jmsTemplate.convertAndSend(StringUtil.records, JSON.toJSONString(new HashMap(){{
                this.put("data",list);
                this.put("wxId",wxId);
            }}));
            return list;
        }
    }
    /**
     * 根据活动id查询所有礼物//这个接口没有定义这个方法
     *
     * @return
     */
    @Override
    public Result getGiftByActivityId(String acId) {
        try {
            List<Map> wxuserGifts = giftMapper.getGiftByActivityId(acId);
            //将查出的数据传到Result 得到返回码
            return Result.ok(wxuserGifts);
        } catch (Exception e) {
            return Result.defeated("");
        }
    }

    /*
     * 查询所有礼物信息
     * */
    @Override
    public Result selectAllGift() {
        List<Gift> gifts = wxuserGiftDao.selectAllGift();
        return Result.ok(gifts);
    }
}
